#include "SLWindow.h"
SLWindow::SLWindow(void)
{
}

SLWindow::~SLWindow(void)
{
}


LRESULT CALLBACK SLWindow::WindowProc( HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam )
{

	switch( uMsg )
	{
	case WM_QUIT:
		break;
	case WM_KEYDOWN:
		{
			switch( wParam )
			{
				case VK_ESCAPE:
					PostQuitMessage(0);
					break;
			}
		}
	case WM_CLOSE:
		{
			//PostQuitMessage(0);	
		}
		break;
	case WM_DESTROY:
		{
			PostQuitMessage(0);
		}
		break;
	default:
		return ::DefWindowProc( hwnd, uMsg, wParam,lParam );
	}

	return 0;
}

bool SLWindow::Initialize( const char * strName, const char * strClass, short sMenuID, short sIconID,
				 HCURSOR hCursor, RECT & rect, unsigned long ulStyle )
{
	m_strWindowClass = strName;
	m_strWindowClass = strClass;
	
	LoadWindowCursor();

	return InitApplication(sMenuID, sIconID,m_hCursor[0]) && InitInstance( ulStyle, rect ) && InitRenderer();
}

bool SLWindow::InitApplication( short sMenuID, short sIconID, HCURSOR hCursor )
{
	WNDCLASSEX		wc;

	memset( &wc, 0, sizeof( WNDCLASSEX ) );

	wc.style         = CS_OWNDC | CS_DBLCLKS;
	wc.hInstance     = ::GetModuleHandle( NULL );
	wc.lpszClassName = m_strWindowClass.c_str();
	wc.lpfnWndProc   = WindowProc;

	wc.lpszMenuName  = MAKEINTRESOURCE( sMenuID );
	wc.hIcon         = ::LoadIcon     ( 0, MAKEINTRESOURCE( sIconID ) );
	wc.hCursor       = hCursor;

	wc.hbrBackground = ( HBRUSH )::GetStockObject( BLACK_BRUSH );
	wc.cbSize        = sizeof( WNDCLASSEX );

	if( NULL == ::RegisterClassEx( &wc ) )
	{
		return false;
	}

	return true;
}

bool SLWindow::InitInstance( unsigned long ulStyle, RECT & rect )
{
	::AdjustWindowRect( ( &rect ), ulStyle, false );

	m_hWnd = ::CreateWindow(
	const_cast< TCHAR * >( m_strWindowClass.c_str() ),
	const_cast< TCHAR * >( m_strWindowName.c_str() ),
	ulStyle,
	0,
	0,
	rect.right - rect.left,
	rect.bottom - rect.top,
	0,
	0,
	::GetModuleHandle( NULL ),
	0 );

	if( NULL == m_hWnd )
	{
		return false;
	}

	return true;

}

bool SLWindow::InitRenderer()
{
	bool bFullScreen = false;
	m_Renderer = sl::Renderer::Create( m_hWnd, bFullScreen );

	if ( !m_Renderer )
		return false;

	return true;
}

void SLWindow::ReleaseDevice()
{
	// how to do this, delete or using the ptr function ?
	sl::Renderer::Destroy( m_Renderer.get() );
}

void SLWindow::MainProcess()
{
	// input the scenedate if there is not any node, we just set default 
	// not input any parament
	m_Renderer->RenderScene(m_rCamera.get());
	// scene render	
	// renderProcess
	
}

void SLWindow::ShowBaseWindow()
{
	if (m_hWnd == NULL )
		return;
	::ShowWindow( m_hWnd, SW_SHOWNORMAL );
}

void SLWindow::MoveCenterWindow()
{
	if ( m_hWnd == NULL )
		return;

	int sx = GetSystemMetrics(SM_CXFULLSCREEN);
	int sy = GetSystemMetrics(SM_CYFULLSCREEN);

	RECT rc;
	::GetWindowRect( m_hWnd, &rc);
	int width = rc.right -rc.left;
	int height = rc.bottom -rc.top;
	int x = (sx - width)/2;
	int y = (sy - height)/2;
	::SetWindowPos( m_hWnd, HWND_TOP, x, y, width, height,SWP_DRAWFRAME | SWP_SHOWWINDOW);
}

void SLWindow::DestroyBaseWindow()
{
	if ( m_hWnd != INVALID_HANDLE_VALUE )
		::DestroyWindow( m_hWnd );	
}

void SLWindow::UpdateBaseWindow()
{
	if( m_hWnd == INVALID_HANDLE_VALUE )
		return;
	::UpdateWindow( m_hWnd );
}


void SLWindow::SetFocusBaseWindow()
{
	if ( m_hWnd == NULL )
		return;

	::SetFocus( m_hWnd );
}


void SLWindow::LoadWindowCursor()
{
	m_hCursor[0] = LoadCursor(NULL, IDC_ARROW );
}

void SLWindow::SetWindowCursor( CURSOR_STATE cursor )
{

}

int SLWindow::PeekMessagePumping()
{
	MSG uMsg;
	memset( &uMsg, 0, sizeof(MSG));
	
	while( uMsg.message != WM_QUIT )
	{
		if( PeekMessage( &uMsg, NULL, 0, 0, PM_REMOVE ) )
		{
			TranslateMessage( &uMsg );
			DispatchMessage( &uMsg );
		}
		else
		{	
			MainProcess();
		}
	}

	ReleaseDevice();

	UnregisterClass( m_strWindowClass.c_str(), ::GetModuleHandle( NULL ) );
	return uMsg.wParam;
}